package com.example.txl.until;

import android.content.Context;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;


// DBUntil类继承自SQLiteOpenHelper，用于管理数据库相关操作
public class DBUntil extends SQLiteOpenHelper {

    // 定义数据库名称
    public static final String DB_NAME = "db.addBook.db";
    // 定义数据库版本号
    private static final int VERSION = 6;
    // 用于操作数据库的SQLiteDatabase对象，初始化为null
    public static SQLiteDatabase db = null;// 用它来操作数据库

    // 构造函数，接受一个Context对象，调用父类构造函数来初始化数据库相关信息
    public DBUntil(Context context) {
        // 调用父类SQLiteOpenHelper的构造函数
        super(context, DB_NAME, null, VERSION,null);
    }

    // 当数据库第一次创建时调用此方法
    @Override
    public void onCreate(SQLiteDatabase db) {
        // 创建一个表格，包含自增主键s_id、姓名s_name、电话s_phone、性别s_sex、备注s_remark字段
        db.execSQL("drop table if exists d_peo");
        db.execSQL("CREATE TABLE d_peo (" +
                "s_id INTEGER primary key AUTOINCREMENT," +
                "s_name varchar(20)," +
                "s_phone varchar(20)," +
                "s_sex varchar(20)," +
                "s_remark varchar(20))");

        db.execSQL("INSERT INTO d_peo (s_name, s_phone, s_sex, s_remark) VALUES('隔壁王叔', '123456789', '男', '开锁')");
        db.execSQL("INSERT INTO d_peo (s_name, s_phone, s_sex, s_remark) VALUES('隔壁李姨', '123456789', '女', '早餐店铺')");
        db.execSQL("INSERT INTO d_peo (s_name, s_phone, s_sex, s_remark) VALUES('C隔壁李姨', '123456789', '女', '早餐店铺')");
        db.execSQL("INSERT INTO d_peo (s_name, s_phone, s_sex, s_remark) VALUES('B隔壁李姨', '123456789', '女', '早餐店铺')");
        db.execSQL("INSERT INTO d_peo (s_name, s_phone, s_sex, s_remark) VALUES('C隔壁李姨', '123456789', '女', '早餐店铺')");
        db.execSQL("INSERT INTO d_peo (s_name, s_phone, s_sex, s_remark) VALUES('11隔壁李姨', '123456789', '女', '早餐店铺')");
        db.execSQL("INSERT INTO d_peo (s_name, s_phone, s_sex, s_remark) VALUES('2隔壁老王', '12345688933', '男', '早餐店铺')");
    }

    // 当数据库版本更新时调用此方法，这里直接重新创建数据库
    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        onCreate(sqLiteDatabase);
    }
}
